package com.dimples.dd.system.mapper;

import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.mybatis.core.mapper.BaseMapperX;
import com.dimples.dd.mybatis.core.query.MPJLambdaWrapperX;
import com.dimples.dd.system.model.bo.SystemOperateLogBO;
import com.dimples.dd.system.model.entity.SystemOperateLogDO;
import com.dimples.dd.system.model.entity.SystemUsersDO;
import com.dimples.dd.system.model.query.OperateLogPageQuery;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface SystemOperateLogMapper extends BaseMapperX<SystemOperateLogDO> {

    /**
     * 分页查询操作日志
     *
     * @param pageQuery 查询参数
     * @return PageResult
     */
    default PageResult<SystemOperateLogBO> selectOperateLogPage(OperateLogPageQuery pageQuery) {
        MPJLambdaWrapperX<SystemOperateLogDO> lambdaWrapperX = new MPJLambdaWrapperX<>();
        lambdaWrapperX.leftJoin(SystemUsersDO.class, SystemUsersDO::getId, SystemOperateLogDO::getUserId)
                .selectAll(SystemOperateLogDO.class)
                .selectAs(SystemUsersDO::getNickname, SystemOperateLogBO::getNickname);

        lambdaWrapperX.eqIfPresent(SystemOperateLogDO::getUserId, pageQuery.getUserId())
                .eqIfPresent(SystemOperateLogDO::getUserType, pageQuery.getUserType())
                .eqIfPresent(SystemOperateLogDO::getApplicationName, pageQuery.getApplicationName())
                .likeIfPresent(SystemOperateLogDO::getRequestUrl, pageQuery.getRequestUrl())
                .betweenIfPresent(SystemOperateLogDO::getBeginTime, pageQuery.getBeginTime())
                .geIfPresent(SystemOperateLogDO::getDuration, pageQuery.getDuration())
                .eqIfPresent(SystemOperateLogDO::getResultCode, pageQuery.getResultCode())
                .eqIfPresent(SystemOperateLogDO::getOperateType, pageQuery.getOperateType())
                .orderByDesc(SystemOperateLogDO::getId);

        return this.selectJoinPage(pageQuery, SystemOperateLogBO.class, lambdaWrapperX);
    }
}